package com.hyt.it.ogt.kq.service.gov.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.kq.common.gov.enums.TaskConfigCodeEnum;
import com.hyt.it.ogt.kq.service.gov.mapper.TaskConfigMapper;
import com.hyt.it.ogt.kq.service.gov.model.entity.TaskConfig;
import com.hyt.it.ogt.kq.service.gov.model.param.TaskOffLineConfig;
import com.hyt.it.ogt.kq.service.gov.model.param.TaskOnLineConfig;
import com.hyt.it.ogt.kq.service.gov.service.IOfficeTaskConfigService;
import com.hyt.it.ogt.kq.service.gov.service.ITaskConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * <p>
 * 考试任务配置表 服务实现类
 * </p>
 *
 * @author huangyh
 * @since 2021-03-08
 */
@Service
public class TaskConfigServiceImpl extends BaseServiceImpl<TaskConfigMapper, TaskConfig> implements ITaskConfigService {

    @Resource
    private IOfficeTaskConfigService iOfficeTaskConfigService;

    @Resource
    private TaskConfigMapper taskConfigMapper;

    @Override
    public List<TaskConfig> getByTaskId(String id) {
        QueryWrapper<TaskConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(TaskConfig::getTaskId, id)
                .eq(TaskConfig::getDelFlag, false);
        return list(queryWrapper);
    }

    @Override
    public List<TaskOffLineConfig> getTaskOffLineConfigByTaskId(String id) {
        List<TaskConfig> taskConfigs = getByTaskId(id);

        List<TaskOffLineConfig> taskOffLineConfigs = new ArrayList<>();
        taskConfigs.stream().forEach(taskConfig -> {
            TaskOffLineConfig taskOffLineConfig = new TaskOffLineConfig();
            BeanUtils.copyProperties(taskConfig,taskOffLineConfig);
            taskOffLineConfig.setConfigCode(taskConfig.getCode())
                            .setConfigValue(taskConfig.getValue());
            taskOffLineConfigs.add(taskOffLineConfig);
        });
        // 需要过滤掉相同configCode的数据
        Map<String, TaskOffLineConfig> coonfigCodeMap = new HashMap<>();
        for (Iterator<TaskOffLineConfig> iterator = taskOffLineConfigs.iterator(); iterator.hasNext();) {
            TaskOffLineConfig pushExamConfigParamTemp =  iterator.next();
            if(coonfigCodeMap.containsKey(pushExamConfigParamTemp.getConfigCode())) {
                iterator.remove();
            }
            coonfigCodeMap.put(pushExamConfigParamTemp.getConfigCode(), pushExamConfigParamTemp);
        }
        return taskOffLineConfigs;
    }

    @Override
    public void updateByCode(List<TaskConfig> taskConfigList) {
        baseMapper.updateByCode(taskConfigList);
    }

    @Override
    public void initTaskConfig(String taskId, String officeId) {

        //任务参数初始化
        Integer count = this.lambdaQuery()
                .eq(TaskConfig::getTaskId,taskId)
                .eq(TaskConfig::getDelFlag,false)
                .count();
        if (count <= 0) {
            //机构参数校验和初始化
            iOfficeTaskConfigService.initOfficeTaskConfig(officeId);
            //任务参数初始化
            baseMapper.initTaskConfig(taskId,officeId);
        }
    }

    @Override
    public String getExamSystemTypeByTaskCode(String taskCode) {
        return taskConfigMapper.getExamSystemTypeByTaskCode(taskCode,
                TaskConfigCodeEnum.EXAM_SYSTEM_TYPE.getCode());
    }

    @Override
    public Optional<String> getExamSystemTypeByTaskId(String taskId) {
        return this.lambdaQuery().select(TaskConfig::getValue)
                .eq(TaskConfig::getTaskId, taskId)
                .eq(TaskConfig::getCode, TaskConfigCodeEnum.EXAM_SYSTEM_TYPE.getCode())
                .eq(TaskConfig::getDelFlag, false)
                .last("LIMIT 1")
                .oneOpt()
                .map(TaskConfig::getValue);
    }

    @Override
    public String getExamSystemVersionByTaskCode(String taskCode) {
        return taskConfigMapper.getExamSystemVersionByTaskCode(taskCode,
                TaskConfigCodeEnum.EXAM_VERSION.getCode());
    }

    @Override
    public void copyTaskConfig(String newId, String oldId, String userName) {
        taskConfigMapper.copyTaskConfig(newId, oldId, userName);
    }

    @Override
    public String getTaskConfigValue(String taskId, String code) {

        TaskConfig taskConfig = this.lambdaQuery()
                .eq(TaskConfig::getTaskId,taskId)
                .eq(TaskConfig::getCode,code)
                .eq(TaskConfig::getDelFlag,false)
                .last("LIMIT 1")
                .one();

        return taskConfig.getValue();
    }

    @Override
    public Boolean obtType(String taskId) {
        String examType = getTaskConfigValue(taskId, TaskConfigCodeEnum.EXAM_SYSTEM_TYPE.getCode());
        final String type = "OBT";
        if (type.equals(examType)) {
            return false;
            //现在统一使用政企标准的区域模板数据。
            //return true;
        }
        return false;
    }

    @Override
    public String getValueByTaskCode(String taskCode, String code) {
        return baseMapper.getValueByTaskCode(taskCode, code);
    }

    @Override
    public List<TaskConfig> listByTaskId(String taskId) {
        return this.lambdaQuery()
                .select(TaskConfig::getCode, TaskConfig::getValue)
                .eq(TaskConfig::getTaskId, taskId)
                .eq(TaskConfig::getDelFlag, false)
                .list();
    }

    @Override
    public List<TaskConfig> listByTaskCode(String taskCode) {
        return baseMapper.listByTaskCode(taskCode);
    }

    @Override
    public String getValueByCode(List<TaskConfig> taskConfigList, String code) {
        return taskConfigList.stream()
                .filter(taskConfig -> code.equals(taskConfig.getCode()))
                .findFirst()
                .map(TaskConfig::getValue)
                .orElse(null);
    }

}
